package org.tp8.sampleapp.camel.msgconverter;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Component;
import org.tp8.sampleapp.util.SampleAppLogger;

/**
 * The Class CourseMsgConverter.
 */
@Component("courseMsgConverter")
public class CourseMsgConverter implements MessageConverter {

	/** The Constant log. */
	private final static SampleAppLogger log = new SampleAppLogger(
			CourseMsgConverter.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.jms.support.converter.MessageConverter#fromMessage
	 * (javax.jms.Message)
	 */
	@Override
	public Object fromMessage(Message message) throws JMSException,
			MessageConversionException {
		Object result;
		if (message instanceof ObjectMessage) {
			ObjectMessage objMessage = (ObjectMessage) message;
			Object object = (Object) objMessage.getObject();
			result = object.toString();
		} else if (message instanceof TextMessage) {
			TextMessage txtMessage = (TextMessage) message;
			result = txtMessage.getText();
		} else if (message instanceof BytesMessage) {
			BytesMessage byteMessage = (BytesMessage) message;
			result = byteMessage.readUTF();
		} else if (message instanceof MapMessage) {
			MapMessage mapMessage = (MapMessage) message;
			result = mapMessage.toString();
		} else {
			throw new MessageConversionException("Unknown JMS Message type");
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.jms.support.converter.MessageConverter#toMessage(
	 * java.lang.Object, javax.jms.Session)
	 */
	@Override
	public Message toMessage(Object obj, Session session) throws JMSException,
			MessageConversionException {
		log.error("Not implemented yet!");
		return null;
	}

}
